const App = getApp();
import Notify from '../../dist/notify/notify';

Page({
    data: {
        imgUrl:App.globalData.imgUrl,
        isLoading: true,
        reLoading: false,
        isFullSucreen: App.globalData.isFullSucreen, //判断机型
    },

    onLoad: function (options) {
        let _this = this;
        _this.getUserDetail();
    },

    onShow() {

    },


    // 获取用户信息判断登陆状态
    getUserDetail() {
        let _this = this;
        App._get('user.index/detail', {}, function (result) {
            _this.setData(result.data);
        }, false, () => {
            _this.setData({
                isLoading: false,
                reLoading: false,
            })
        });
    },

    // 提交表单
    submit(e) {
        let _this = this,
            value = e.detail.value;
        // 表单校验
        if (!_this._onVerify(value)) {
            return false
        }
        _this.setData({
            reLoading: true,
        })
        App._post_form('card/create', value, res => {
            wx.showToast({
                title: res.msg,
                icon: 'success',
                duration: 2000,
                success: function () {
                    wx.navigateBack();
                }
            })
        }, false, () => {
            _this.setData({
                reLoading: false,
            })
        });
    },

    // 表单验证
    _onVerify(data) {
        let _this = this;
        if (!data.num) {
            Notify({type: 'primary', message: '请输入要生成的卡密数量'});
            return false
        }
        if (data.num <= 0) {
            Notify({type: 'primary', message: '卡密数量必须大于0'});
            return false
        }
        if (data.num > 20) {
            Notify({type: 'primary', message: '每次生成最大数量限制为20张'});
            return false
        }
        if (!data.expire) {
            Notify({type: 'primary', message: '请输入开通VIP的期限时长'});
            return false
        }
        if (data.expire <= 0) {
            Notify({type: 'primary', message: 'VIP时长必须大于0'});
            return false
        }
        if (data.expire > _this.data.userInfo.max_month) {
            Notify({type: 'primary', message: '您最大生成的时长为: ' + _this.data.userInfo.max_month + ' 个月'});
            return false
        }
        return true;

    },


});
